<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/

class c_main_class extends AWS_CONTROLLER
{
	public $current_uid;
	public $action_key_val;

	public function get_access_rule()
	{
		$rule_action["rule_type"] = 'white'; //黑名单,黑名单中的检查  'white'白名单,白名单以外的检查
		$rule_action["actions"] = array();
		
		return $rule_action;
	}

	public function setup()
	{
		if (isset($_GET['notification_id']))
		{
			$this->model('notify')->read_notification($_GET['notification_id']);
		}
		
		$this->action_key_val = ACTION_LOG::$ACTION_STRING_ARRAY; //注释调用ACTION_LOG动作表

		if (intval($_GET["uid"]))
		{
			$this->current_uid = intval($_GET["uid"]);
		}
		else
		{
			if ((! $_GET['u']) && ($_GET['id']))
			{
				$_GET['u'] = $_GET['id'];
			}
			
			$this->current_uid = $this->model('account')->get_uid_by_user_name($_GET["u"]);
			
			if (! $this->current_uid)
			{
				$this->current_uid = $this->user_id;
			}
		}
		
		$this->current_uid = intval($this->current_uid);
		
		if (! $this->current_uid)
		{
			HTTP::redirect('/');
		}
	}

	/**
	 * 默认运作,显示个人首页
	 */
	function index_action()
	{
		if ($this->current_uid == $this->user_id)
		{
			$user = $this->user_info;
		}
		else
		{
			$user = $this->data_cache('account_get_users_' . $this->current_uid . "_true", '$this->model("account")->get_users(' . $this->current_uid . ', true)', get_setting('cache_level_high'), "g_uid_" . $this->current_uid);
			
			$this->model('people')->update_views_count($user["uid"]);
		}
		
		$focus_topics = $this->data_cache('topic_get_focus_topic_list_' . $user['uid'] . '_10', '$this->model("topic")->get_focus_topic_list(false, ' . $user['uid'] . ', 10)', get_setting('cache_level_high'));
		$side_fans_list = $this->data_cache('follow_get_user_fans_5' . $this->current_uid, '$this->model("follow")->get_user_fans(' . $this->current_uid . ',5)', get_setting('cache_level_high'), "g_uid_" . $this->current_uid);
		$side_friends_list = $this->data_cache('follow_get_user_friends_5' . $this->current_uid, '$this->model("follow")->get_user_friends(' . $this->current_uid . ',5)', get_setting('cache_level_high'), "g_uid_" . $this->current_uid);
		
		//$in_integral_top = $this->model('people')->in_integral_top_list($user["uid"]);
		
		$reputation_topics = $this->model('reputation')->get_reputation_topic($this->current_uid);
		
		if ($reputation_topics)
		{
			$topic_all_count = 0;
			
			foreach ($reputation_topics as $rtkey => $rtval)
			{
				$topic_all_count += $rtval['topic_count'];
				$rtopic_ids[] = $rtval['topic_id'];
			}
			
			if($rtopic_row = $this->model('topic')->get_topic_title($rtopic_ids))
			{
				foreach ($rtopic_row as $row_key => $row_val)
				{
					$rtopic[$row_val['topic_id']] = $row_val['topic_title'];
				}
			}
			foreach ($reputation_topics as $rtkey => $rtval)
			{
				$reputation_topics[$rtkey]['percent'] = intval($rtval['topic_count'] / $topic_all_count * 100);
				$reputation_topics[$rtkey]['title'] = $rtopic[$rtval['topic_id']];
			}
			
			$reputation_topics = array_slice($reputation_topics, 0, 10);
			
			TPL::assign('reputation_topics', $reputation_topics);
		
		}
		
		if ($user['uid'] == $this->user_id)
		{
			$user["ta"] = '我';
		}
		else
		{
			$user["ta"] = 'TA';
		}
		
		$area_tmp = $this->model('account')->get_user_areas_by_uids(array(
			$this->current_uid
		));
		
		TPL::assign('area_name', $area_tmp[$this->current_uid]);
		TPL::assign('user', $user);
		
		$job_info = $this->model('account')->get_jobs_by_id($user['job_id']);
		TPL::assign('jobs_name', $job_info["jobs_name"]);
		
		if($focus_topics)
		{
			foreach ($focus_topics as $key => $val)
			{
				$focus_topics[$key]["has_focus"] = $this->model("topic")->has_focus_topic($this->user_id, $val["topic_id"]);
			}
		}
		if ($users_sina = $this->model('sina_weibo')->get_users_sina_by_uid($this->current_uid))
		{
			TPL::assign('sina_weibo_url', 'http://www.weibo.com/' . $users_sina['id']);
		}
		
		if ($users_qq = $this->model('qq_weibo')->get_users_qq_by_uid($this->current_uid))
		{
			TPL::assign('qq_weibo_url', 'http://t.qq.com/' . $users_qq['name']);
		}
		
		TPL::assign('user_follow_check', $this->model('follow')->user_follow_check($this->user_id, $this->current_uid));
		TPL::assign('side_fans_list', $side_fans_list);
		TPL::assign('side_friends_list', $side_friends_list);
		TPL::assign('focus_topics', $focus_topics);
		
		$this->crumb($user['user_name'] . ' 的个人主页', 'people/' . $user['user_name']);
		
		TPL::import_css('css/discussion.css');
		TPL::import_css('css/bank_Cash.css');
		
		TPL::output("people/index");
	}
}